<template>
  <view>
    <my-search></my-search>
    <view class="cate-box">
      <!-- 左侧盒子 -->
      <scroll-view class="left-box" scroll-y :style="{ height: wh + 'px' }">
        <view
          :class="['left-item', index === activeIndex ? 'active' : '']"
          v-for="(item, index) in cateList"
          :key="item.cat_id"
          @click="changeTab(index)"
        >
          {{ item.cat_name }}
        </view>
      </scroll-view>
      <!-- 右侧盒子 -->
      <scroll-view
        class="right-box"
        scroll-y
        :style="{ height: wh + 'px' }"
        :scroll-top="scrollTop"
      >
        <view
          class="cate-lv2"
          v-for="(item2, index2) in cateList2"
          :key="item2.cat_id"
        >
          <!-- 二级标题区: -->
          <view class="cate-lv2-title">/ {{ item2.cat_name }} /</view>

          <!-- 三级图片区 :-->
          <view class="cate-lv3-list">
            <view
              class="cate-lv3-item"
              v-for="(item3, index3) in item2.children"
              :key="item3.cat_id"
              @click="gotoGoodsList(item3.cat_id)"
            >
              <image :src="item3.cat_icon" mode=""></image>
              <text>{{ item3.cat_name }}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import badgeMixins from '@/mixins/tabBar-badge.js';
export default {
  mixins: [badgeMixins],
  data() {
    return {
      wh: 0,
      // 一级分类列表
      cateList: [],
      // 二级分类
      cateList2: [],
      // 当前被选中项的索引,默认让第一项被选中
      activeIndex: 0,
      scrollTop: 0
    };
  },
  onLoad() {
    // 获取屏幕可用高度：不包含NavigationBar和TabBar的高度
    // console.log(uni.getSystemInfoSync().windowHeight);
    this.wh = uni.getSystemInfoSync().windowHeight - 60;
    // 请求分类列表
    this.getCateList();
  },
  methods: {
    // 跳转商品列表页
    gotoGoodsList(id) {
      uni.navigateTo({
        url: '/subpkg/goods_list/goods_list?cid=' + id
      });
    },
    // 切换一级分类的tab
    changeTab(index) {
      this.activeIndex = index;
      // 替换二级分类那个数组的内容
      this.cateList2 = this.cateList[index].children;
      // 切换tab回到顶部
      this.scrollTop = this.scrollTop === 1 ? 0 : 1;
    },
    async getCateList() {
      const { data } = await uni.$http.get('/api/public/v1/categories');
      console.log(data);
      if (data.meta.status !== 200) {
        return uni.$showMsg();
      }
      // 一级分类的赋值
      this.cateList = data.message;
      // 二级分类赋值
      this.cateList2 = data.message[0].children;
    }
  }
};
</script>

<style lang="scss">
.cate-box {
  display: flex;
  .left-box {
    width: 250rpx;
    .left-item {
      line-height: 60px;
      background-color: #efefef;
      font-size: 14px;
      text-align: center;
      &.active {
        background-color: #fff;
        position: relative;
        &::before {
          position: absolute;
          left: 0;
          top: 0;
          bottom: 0;
          margin: auto;
          content: '';
          display: block;
          width: 4px;
          height: 30px;
          background-color: #c00000;
        }
      }
    }
  }
  .right-box {
    .cate-lv2-title {
      text-align: center;
      font-weight: bold;
      font-size: 12px;
    }
    .cate-lv3-list {
      display: flex;
      flex-wrap: wrap;
      .cate-lv3-item {
        width: 33.3%;
        display: flex;
        flex-direction: column;
        align-items: center;
        image {
          width: 60px;
          height: 60px;
        }
        text {
          font-size: 12px;
        }
      }
    }
  }
}
</style>
